import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_learn/common/routes/app_pages.dart';
import 'package:getx_learn/pages/list_detail/index.dart';

class HomeView extends StatelessWidget {
  const HomeView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("首页"),
      ),
      body: ListView(
        children: [
          // 路由导航
          ListTile(
            title: Text("导航-命名路由 home->list"),
            subtitle: Text('Get.toNamed("/home/list")'),
            onTap: () => Get.toNamed("/home/list"),
          ),

          ListTile(
            title: Text("导航-命名路由 home->list->detail"),
            subtitle: Text('Get.toNamed("/home/list/detail")'),
            onTap: () => Get.toNamed("/home/list/detail"),
          ),

          ListTile(
            title: Text("导航->类对象"),
            subtitle: Text("Get.to(DetailView())"),
            onTap: () => Get.to(DetailView()),
          ),

          // 相当于 A Activity 跳转 B Activity，并且 finish 了 A
          ListTile(
            title: Text("导航->清除上一级"),
            subtitle: Text("Get.off(DetailView())"),
            onTap: () => Get.off(DetailView()),
          ),

          // 清除所有，相当于 Activity 的启动置顶模式，就是清除栈
          ListTile(
            title: Text("导航->清除所有"),
            subtitle: Text("Get.offAll(DetailView())"),
            onTap: () => Get.offAll(DetailView()),
          ),

          ListTile(
            title: Text("导航-arguments传值+返回值"),
            subtitle: Text(
                'Get.toNamed("/home/list/detail", arguments: {"id": 999})'),
            onTap: () async {
              var result = await Get.toNamed("/home/list/detail",
                  arguments: {"id": 8888});
              if(result != null){
                Get.snackbar("返回值", "success => " + result["success"].toString());
              }
            },
          ),


          ListTile(
            title: Text("导航-parameters传值+返回值"),
            subtitle: Text('Get.toNamed("/home/list/detail?id=666")'),
            onTap: () async {
              var result = await Get.toNamed("/home/list/detail?id=666");
              if (result != null){
                Get.snackbar("返回值", "success -> " + result["success"].toString());
              }

            },
          ),

          ListTile(
            title: Text("导航-参数传值+返回值"),
            subtitle: Text('Get.toNamed("/home/list/detail/777")'),
            onTap: () async {
              var result = await Get.toNamed("/home/list/detail/777");
              if (result != null){
                Get.snackbar("返回值", "success -> " + result["success"].toString());
              }
            },
          ),


          ListTile(
            title: Text("导航-not found"),
            subtitle: Text('Get.toNamed("/home/list/detail/777/33")'),
            onTap: ()  {
              // 如果路由有多级，例如：/home/list/detail 这里有三级路由，
              // 假设 home 写错，就会跳转未找到页面；
              // 假设 list 那级写错，就会跳转到上级页面，依次类推
              // Get.toNamed("/home/list/detail");

              // 按这种因为匹配到 /detail/:id 这种，会解析错误
               // Get.toNamed("/home/list/detail/777/33");

              // 从一级开始错误，就跳转未找到页面
               Get.toNamed("/aa/bb/cc/");
            },
          ),

          ListTile(
            title: Text("导航-中间件-认证Auth"),
            subtitle: Text('Get.toNamed(AppRoutes.My)'),
            onTap: () => Get.toNamed(AppRoutes.My),
          ),

          Divider(),
        ],
      ),
    );
  }
}
